from django.db import models
from django.contrib.auth.models import User
from user.models import AuditModelMixin


class Repository(AuditModelMixin, models.Model):
    name = models.CharField(max_length=64, unique=True)
    git_url = models.CharField(max_length=256, default='')

    class Meta:
        ordering = ["name"]


class TaskState(models.IntegerChoices):
    PENDING = 0, 'PENDING'
    RUNNING = 1, 'RUNNING'
    COMPLETED = 2, 'COMPLETED'
    FAILED = 3, 'FAILED'
    CANCELED = 4, 'CANCELED'
    TIMEOUT = 5, 'TIMEOUT'
    CANCELING = 6, 'CANCELING'


class Task(AuditModelMixin, models.Model):
    repository = models.ForeignKey(Repository, on_delete=models.PROTECT)
    playbook = models.CharField(max_length=64, default="site.yml")
    commit_id = models.CharField(max_length=40, default='')
    state = models.IntegerField(default=TaskState.PENDING)
    output = models.TextField(default='')